//
//  PwdViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/18.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "PwdViewController.h"

@interface PwdViewController ()
{
    NSTimer *timer;//定时器
    int count;//记录时间
}

@property (weak, nonatomic) IBOutlet UITextField *oldPwd;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *xinPwd;
@property (weak, nonatomic) IBOutlet UITextField *timeTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@property(nonatomic,copy)NSString *codeStr;
@end

@implementation PwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.timeTextField.hidden = YES;
    self.timeTextField.enabled = NO;
    self.codeBtn.hidden = NO;
    if(!ISNOTNEEDMESSAGEBTN){
        self.isNeedMessageItem = YES;
    }
    self.oldPwd.secureTextEntry = YES;
    self.xinPwd.secureTextEntry = YES;
    self.codeTextField.keyboardType = UIKeyboardTypeNumberPad;
}

- (IBAction)getCode:(id)sender {
    if(!USERPHONE.length){
        [MBProgressHUD showTipMessageInWindow:@"请先绑定手机号"];
        return;
    }
    //验证码
    NSDictionary *paramDic = @{@"phoneNum":USERPHONE};
    __weak typeof(self)  weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:GETCODE paramters:paramDic afterResultBlock:^(NSDictionary *resultDic) {
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            [MBProgressHUD showTipMessageInWindow:@"验证码发送成功"];
            DLog(@"验证吗--%@",resultDic);
            //,59秒倒计时
            [weakSelf timer59Seconds];
            NSDictionary *dataDic = resultDic[@"data"];
            weakSelf.codeStr =dataDic[@"CAPTCHA"];
            [weakSelf.codeTextField becomeFirstResponder];
        }else{
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

//4,59秒倒计时
- (void)timer59Seconds{
    self.codeBtn.hidden = YES;
    self.timeTextField.hidden = NO;
    if (!timer) {
        count = 59;
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
    }
}
- (void)changeTime{
    count--;
    self.timeTextField.text = [NSString stringWithFormat:@"%02d秒后可重新获取",count];
    if(count==0){
        [timer invalidate];
        timer = nil;
        self.codeBtn.hidden = NO;
        [self.codeBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
        self.timeTextField.hidden = YES;
    }
}

- (IBAction)commit:(id)sender {
    if(!_oldPwd.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请输入新密码"];
        return;
    }
    if(self.oldPwd.text.length<6||self.oldPwd.text.length>16){
        [MBProgressHUD showTipMessageInWindow:@"请输入6-16位密码"];
        return;
    }

    if(!_codeTextField.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请输入验证码"];
        return;
    }
    if(!_xinPwd.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请再输入新密码"];
        return;
    }
    
    if(![_oldPwd.text isEqualToString:_xinPwd.text]){
        [MBProgressHUD showTipMessageInWindow:@"两次密码输入不一致"];
        return;
    }

    // 2.50更改密码接口
    NSString *userName = [AppDelegate appDealegate].huanxinUserName;
    NSDictionary *paraDic = @{
                              @"uid":USERID,
                              @"passWord":self.xinPwd.text,
                              @"userName":userName
                              };
        __weak typeof(self)  weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:CHANGEPASSWORD paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"更改密码---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic isKindOfClass:[NSNull class]]) return;
            if([dataDic[@"status"] isEqualToString:@"true"]){
                [MBProgressHUD showTipMessageInWindow:@"密码修改成功"];
                [weakSelf performSelector:@selector(popToSettingVc) withObject:nil afterDelay:1];
            }else{
                if(dataDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:dataDic[@"errorString"]];
                }
            }
        }else{
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

-(void)popToSettingVc{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
